<?php
    session_start();
    include_once('connect.php');
    class shopping_cart{
        var $_SESSION = array();
        var $product_id;
        function request_ajax(){
            $product_id = $_POST['product_id'];
        }
        function add_item($pro_id){
            $_SESSION['product'][] = array(
                'pro_id'=>$pro_id,
                'num_pro'=>1,
            );
            
        }
        function clear_one_item($item){
            unset($_SESSION['product'][$item]);
        }
        function clear_all_item(){
            unset($_SESSION['product']);
        }
        function destroy_cart(){
            session_destroy();
        }
        function unset_cart(){
            unset($_SESSION);
        }
        function implode(){
            implode(',',$_SESSION['product']);
        }
        
    }
    $status = 0;
    $product_id = $_POST['product_id'];
        
    $db = new database("SELECT pro_quantity
                        FROM product
                        WHERE pro_id = ".$product_id);
    $db2 = $db->render();
    $num_pro = $db2[0]['pro_quantity'];
    
    $ses = new shopping_cart;
    if(!isset($_SESSION['product'])){
        $_SESSION['product'] = array();
    }
    $check = 0;
    foreach($_SESSION['product'] as $key => $value ){
        if($value['pro_id'] == $product_id){
            if($_SESSION['product'][$key]['num_pro'] < $num_pro){
                $_SESSION['product'][$key]['num_pro'] = $_SESSION['product'][$key]['num_pro'] + 1 ;
            }
            else $status = 1;
            $check = 1;
        }
    }
    if($check != 1){
        if($num_pro > 0){
            $ses->add_item($product_id);    
        }
        else{
            $status = 1;
        }
    }
    echo($status);
    //$ses->clear_one_item(8);
    //$ses->clear_all_item();
    //echo('Ban da mua san pham co ID : '.$product_id);
    //echo($product_id);

    
    
    
    
?>